/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.wii.elpro.web.action;

import com.wii.elpro.dao.SpdcfDAOInterface;
import com.wii.elpro.dao.hb.BusinessPartnerDAOImpl;
import com.wii.elpro.dao.hb.SpdcfDAOImpl;
import com.wii.elpro.entity.Inquery;
import com.wii.elpro.entity.InqueryItem;
import com.wii.elpro.entity.Spdcf;
import com.wii.elpro.entity.SpdcfItem;
import com.wii.elpro.entity.User;
import com.wii.elpro.web.form.SpdcfAllForm;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
 *
 * @author City_Z
 */
public class SpdcfSaveAllAction extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            {
        HttpSession sess = request.getSession(true);
        User user = (User)sess.getAttribute("userSession");       
        if(user==null){
            return mapping.findForward("gagalLogin");            
        }  
        SpdcfAllForm f = (SpdcfAllForm)form;
        SpdcfDAOInterface dao = new SpdcfDAOImpl();       
        Spdcf spdcf = new Spdcf();
        List list = new ArrayList();
        try{
//          it is not suppose to be like this---
          Inquery in1 = (Inquery)sess.getAttribute("inquery");
//          -----
          spdcf.setInquery(in1);
          spdcf.setCustomer(new BusinessPartnerDAOImpl().getBusinessPartner(f.getIdBusinessPartner()));
          spdcf.setAlamatPengiriman(f.getAlamatPengiriman());
          spdcf.setTglSdpcf(new Date());
          spdcf.setNote(f.getNote());
          spdcf.setTransportation(f.getTransportation());
          spdcf.setPpn(f.getPpn());
          spdcf.setValidityPrice(f.getValidityPrice());
          spdcf.setValidityStockLE(f.getValidityStockLE());
          spdcf.setProductionTime(f.getProductionTime());
          spdcf.setProductionTimeDays(f.getProductionTimeDays());
          spdcf.setProductionCondition(f.getProductionCondition());
          spdcf.setFinishingDays(f.getFinishingDays());
          spdcf.setDeliveryMode(f.getDeliveryMode());
          spdcf.setDeliveryModeDesc(f.getDeliveryModeDesc());
          spdcf.setDeliveryTime(f.getDeliveryTime());
          spdcf.setDeliveryTimeDays(f.getDeliveryTimeDays());
          spdcf.setWasKonfirmByFinance(Byte.parseByte("0"));
          spdcf.setWasKonfirmByProduction(Byte.parseByte("0"));  
          if(sess.getAttribute("listSpdcfItem")!=null){
              list = (List)sess.getAttribute("listSpdcfItem");
          }
          spdcf.setItems(list);
          dao.save(spdcf);
          sess.removeAttribute("inquery");
          sess.removeAttribute("listSpdcfItem");
        }catch(Exception ex){
            ex.printStackTrace();
        }
        
        return mapping.findForward(SUCCESS);
        
    }
}